/*
 * Copyright (C) 2021 The Chinese Software International Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.tonyodev.fetch2;

import ohos.utils.Parcel;
import ohos.utils.Sequenceable;

import com.tonyodev.fetch2.util.Defaults;
import com.tonyodev.fetch2core.Extras;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;

/**
 * description RequestInfo
 *
 * @author liuluming
 * @since 2021-05-20
 */
public class RequestInfo implements Sequenceable {
    private long identifier;
    private int groupId;

    private Map<String, String> headers;

    private Priority priority;

    private NetworkType networkType;

    private String tag="";

    private EnqueueAction enqueueAction;

    private boolean downloadOnEnqueue;

    private int autoRetryMaxAttempts;

    private Extras extras;

    public long getIdentifier() {
        return this.identifier;
    }

    public void setIdentifier(long mIdentifier) {
        this.identifier = mIdentifier;
    }

    public int getGroupId() {
        return this.groupId;
    }

    public void setGroupId(int mGroupId) {
        this.groupId = mGroupId;
    }


    public Map<String, String> getHeaders() {
        return this.headers;
    }

    public void setHeaders(Map<String, String> headers) {
        this.headers = headers;
    }

    public Priority getPriority() {
        return this.priority;
    }

    public void setPriority(Priority mPriority) {
        this.priority = mPriority;
    }

    public NetworkType getNetworkType() {
        return this.networkType;
    }

    public void setNetworkType(NetworkType mNetworkType) {
        this.networkType = mNetworkType;
    }

    public void addHeader(Object key, String value) {
        this.headers.put((String) key, value);
    }

    public String getTag() {
        return this.tag;
    }

    public void setTag(String var1) {
        this.tag = var1;
    }

    public EnqueueAction getEnqueueAction() {
        return this.enqueueAction;
    }

    public void setEnqueueAction(EnqueueAction mEnqueueAction) {
        this.enqueueAction = mEnqueueAction;
    }

    public boolean getDownloadOnEnqueue() {
        return this.downloadOnEnqueue;
    }

    public void setDownloadOnEnqueue(boolean var1) {
        this.downloadOnEnqueue = var1;
    }

    public int getAutoRetryMaxAttempts() {
        return this.autoRetryMaxAttempts;
    }

    public void setAutoRetryMaxAttempts(int value) {
        if (value < 0) {
        } else {
            this.autoRetryMaxAttempts = value;
        }
    }

    public Extras getExtras() {
        return this.extras;
    }

    public void setExtras(Extras value) {
        this.extras = value.copy();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        RequestInfo that = (RequestInfo) o;
        return identifier == that.identifier &&
            groupId == that.groupId &&
            downloadOnEnqueue == that.downloadOnEnqueue &&
            autoRetryMaxAttempts == that.autoRetryMaxAttempts &&
            headers.equals(that.headers) &&
            priority == that.priority &&
            networkType == that.networkType &&
            tag.equals(that.tag) &&
            extras.equals(that.extras);
    }

    @Override
    public int hashCode() {
        return Objects.hash(identifier, groupId, headers, priority, networkType, tag, downloadOnEnqueue, autoRetryMaxAttempts, extras);
    }

    @Override
    public boolean hasFileDescriptor() {

        return false;
    }

    public static Sequenceable.Producer
        PRODUCER = new Sequenceable.Producer
        () {
        public RequestInfo createFromParcel(Parcel in) {    //从包裹中获取数据构造对象
            RequestInfo instance = new RequestInfo();
            instance.setGroupId(in.readInt());
            instance.setIdentifier(in.readLong());
            Map<String, String> headers = (Map<String, String>) in.readMap();
            instance.setTag(in.readString());
            Priority priority = Priority.valueOf(in.readInt());
            NetworkType networkType = NetworkType.valueOf(in.readInt());
            instance.setHeaders(headers);
            instance.setPriority(priority);
            instance.setNetworkType(networkType);
            instance.unmarshalling(in);
            return instance;
        }
    };

    @Override
    public boolean marshalling(Parcel out) {
        return out.writeLong(identifier) && out.writeInt(groupId) && out.writeString(tag)
            && out.writeBoolean(downloadOnEnqueue) && out.writeInt(autoRetryMaxAttempts);
    }

    @Override
    public boolean unmarshalling(Parcel in) {
        this.autoRetryMaxAttempts = in.readInt();
        this.downloadOnEnqueue = in.readBoolean();
        this.extras = in.readSerializable(Extras.class);
        this.groupId = in.readInt();
        this.headers = (Map<String, String>) in.readMap();
        this.identifier = in.readLong();
        this.networkType = networkType;
        this.priority = priority;
        this.tag = in.readString();
        return true;
    }

    @Override
    public String toString() {
        return "RequestInfo{" +
            "identifier=" + identifier +
            ", groupId=" + groupId +
            ", headers=" + headers +
            ", priority=" + priority +
            ", networkType=" + networkType +
            ", tag='" + tag + '\'' +
            ", downloadOnEnqueue=" + downloadOnEnqueue +
            ", autoRetryMaxAttempts=" + autoRetryMaxAttempts +
            ", extras=" + extras +
            '}';
    }

    public RequestInfo() {
        Map<String, String> map = new LinkedHashMap();
        this.headers = map;
        this.priority = Defaults.getDefaultPriority();
        this.networkType = Defaults.getDefaultNetworkType();
        this.enqueueAction = Defaults.getDefaultEnqueueAction();
        this.downloadOnEnqueue = true;
        this.extras = Extras.getEmptyExtras();
    }
}
